import {
    ElMessage,
    ElMessageBox
} from 'element-plus'
import NProgress from 'nprogress' // 引入nprogress插件
import {
    logout
} from '@/api/manager'
import {
    removeToken
} from '@/composible/cookie';
import {
    notify
} from '@/composible/notify'
import {
    router
} from '@/router';
import {
    usemainStore
} from '@/stores/mainStore';

//退出登录弹窗
export function logoutPrompt(message = "确认要退出登录吗？", type = "Warning") {
    //返回一个promise对象
    return ElMessageBox.confirm(
        message,
        type, {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type,
        }
    )
}

//进度条加载
export function startLoading() {
    NProgress.start();
}

//进度条关闭
export function doneLoading() {
    NProgress.done();
}

//退出登录方法
export function Logout() {
    //退出登录
    //弹窗
    logoutPrompt().then(() => {
        //发出退出请求
        logout().finally(res => {
            //清除cookie
            //改变pinia的状态
            //直接在mainStore里面设置方法来做
            const mainStore = usemainStore();
            mainStore.logout();
            //跳转到登录页
            router.push('/login');
            location.reload();
            //显示退出登录成功
            notify("success", "退出登录成功");
        })
    })

}

export function showPrompt(tip, value="") {

    return ElMessageBox.prompt(tip, {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        inputValue: value
    });

}